from typing import List

from Melodie import Agent

from MelodieAllegro.builtin_classes import AgentList, Array


class Scenario:
    prop_a: int
    prop_b: float
    account: int

    def __init__(self):
        self.prop_a = 0
        self.prop_b = 0.0


class MyAgent(Agent):
    scenario: Scenario
    c: int
    account: int

    # id: int

    # def __init__(self, agent_id: int):
    #     self.c = 0
    #     self.id = agent_id
    #     self.scenario = Scenario()
    #     self.account = 0

    def parent_call(self, a: int) -> int:
        x: int = 1 + a
        return x

    def produce(self) -> int:
        account: int = self.account + 100
        self.account = account
        return self.account


class ModelProxy:
    agents: AgentList[MyAgent]

    def agents_step2(self, agents: AgentList[MyAgent]) -> int:
        i: int = 0
        sum: int = 0
        j: int = 0
        agent_indices: List[int] = List()
        agent_indices_arr: Array[int] = [0]
        agent1: MyAgent = MyAgent(0)
        agent2: MyAgent = MyAgent(2)
        tmp_agent: MyAgent = MyAgent(0)
        a0: int = 0
        a1: int = 1
        trade_num: int = 2000
        while j < 1:
            i = 0
            sum = j
            while i < agents.agents.length:
                tmp_agent = agents[i]
                tmp_agent.produce()
                i = i + 1
            i = 0
            while i < trade_num:

                agent_indices_arr = sample_index(agents.agents.length, 2)
                a0 = agent_indices_arr[0]
                a1 = agent_indices_arr[1]

                agent1 = agents[a0]
                agent2 = agents[a1]
                if random() > 0.8:
                    if agent1.account > agent2.account:
                        agent1.account = agent1.account + 1
                        agent2.account = agent2.account - 1
                    else:
                        agent1.account = agent1.account - 1
                        agent2.account = agent2.account + 1
                else:
                    if agent1.account <= agent2.account:
                        agent1.account = agent1.account + 1
                        agent2.account = agent2.account - 1
                    else:
                        agent1.account = agent1.account - 1
                        agent2.account = agent2.account + 1
                i = i + 1
            i = 0
            while i < agents.agents.length:
                sum = sum + agents[i].account
                i = i + 1
            j = j + 1
        return sum

    def run(self) -> int:
        l123123: AgentList[MyAgent] = self.agents
        print('refcnt-agents', refcnt(self.agents))
        step: int = 0
        while step < 200:
            self.agents_step2(l123123)  # self.agents)
            step = step + 1
        print('executed!', refcnt(l123123))
        self = ModelProxy()
        return 1
